package com.zf.gateway.downstream.controller;

import com.zf.common.core.result.RestResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;

/**
 * 测试 RestTemplate:
 * <pre>
 * RestTemplate调用结果注意事项：
 *    1、对于响应类型是 Content-Type:"application/*"：
 *        会使用 MappingJackson2XmlHttpMessageConverter 转换器，该转换器使用"UTF-8"编码，可以正常解析UTF-8，中文不会乱码
 *    2、对于响应类型是 Content-Type:"text/*"：
 *        会使用 StringHttpMessageConverter 转换器，该转换器使用"ISO-8859-1"编码, 不能正常解析UTF-8, 中文会乱码，
 *        可以使用 byte[] 接收二进制内容，然后手动转换 new String(byte[], StandardCharsets.UTF_8);
 * <pre/>
 */
@Slf4j
@RestController
@RequestMapping("/rest")
public class RestTemplateTestController {

    @Qualifier("restTemplateByUri")
    @Autowired
    private RestTemplate restTemplateByUri;

    @Qualifier("restTemplateByServiceId")
    @Autowired
    private RestTemplate restTemplateByServiceId;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @GetMapping("/testServiceId")
    public RestResponse<String> testServiceId() {
        // 通过服务名调用
        // String res = restTemplateByServiceId.getForObject("http://gateway-downstream/test/hello", String.class);
        ResponseEntity<String> res = restTemplateByServiceId.getForEntity("http://gateway-downstream/test/hello", String.class);
        System.out.println(res.getHeaders());
        log.info(res.getBody());
        return RestResponse.success(res.getBody());
    }

    @GetMapping("/testUri")
    public RestResponse<String> testUri() {
        // 请求头
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.AUTHORIZATION, "123456");

        // 通过uri调用
        ResponseEntity<byte[]> res = restTemplateByUri.exchange("https://www.baidu.com/", HttpMethod.GET, new HttpEntity<>(null, headers), byte[].class);
        System.out.println(res.getHeaders());
        byte[] body = res.getBody();
        if (body == null || body.length == 0) {
            return RestResponse.success();
        }
        String str = new String(body, StandardCharsets.UTF_8);

        log.info(str);
        return RestResponse.success(str);
    }

    @GetMapping("/testLoadBalancerClient")
    public RestResponse<String> testLoadBalancerClient() {
        // 手动选择服务
        ServiceInstance serviceInstance = loadBalancerClient.choose("gateway-downstream");
        // 拼接URL
        String url = String.format("http://%s:%s/test/hello", serviceInstance.getHost(), serviceInstance.getPort());
        String res = restTemplateByUri.getForObject(url, String.class);
        log.info(res);
        return RestResponse.success(res);
    }

}
